package cn.xaut.数组;

public class demo125 {

    static boolean isNotValidLetter(char c) {
        return (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9');
    }
    
    // 双指针（对撞指针）
    public boolean isPalindrome(String s) {

        if (s.equals(""))
            return true;
        
        String ss = s.toLowerCase();
        char[] cs = ss.toCharArray();
        int l = 0, r = ss.length() - 1;
        while (r > l) {
            while (r > l && isNotValidLetter(cs[l]))
                l ++;
            while (r > l && isNotValidLetter(cs[r])) 
                r --;
            if (cs[l] != cs[r])
                return false;
            l ++;
            r --;
        }
        
        return true;
    }

    public static void main(String[] args) {

        System.out.println(new demo125().isPalindrome("A man, a plan, a canal: Panama"));//true
        System.out.println(new demo125().isPalindrome("race a car"));//false
        System.out.println(new demo125().isPalindrome(".,"));//true
        System.out.println(new demo125().isPalindrome("0P"));//false
    }
}
